<template>
  <div class="inventoryList auditCmpt">
    <!-- 查询表单 -->
    <div class="form-baseinfo">
      <LPSearchBar
        v-model="searchModel"
        :search-data="searchData"
        label-width="100px"
        @handleSearchSubmit="handleSearchSubmit"
        @handleSearchReset="handleSearchReset"
      />
    </div>
    <div class="form-baseinfo">
      <div class="btn_area">
        <el-button size="small" type="primary" @click="exportInfo">导出数据</el-button>
      </div>
      <!-- 表格列表 -->
      <LPTablePage
        :show-index="false"
        :show-indexfixed="false"
        index-label="顺序"
        :table-data="tableData"
        :table-column="tableColumn"
        :total="total"
        :current-page="currentPage"
        align="center"
        class="table"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
        <template slot="ordersNo" slot-scope="{ row }">
          <el-link type="primary" @click="clickSoNO(row)">{{ row.ordersNo }}</el-link>
        </template>
      </LPTablePage>
    </div>
  </div>
</template>

<script>
import { getByIdOrderData } from '@/api/LaborProtection/order'
import { getProductInventoryLogList } from '@/api/LaborProtection/productInventoryLog'
import { GetProductInventoryLogExcel } from '@/api/LaborProtection/export'
import { download, parseTime } from '@/utils/lpUtils'
import LPTablePage from '@/components/LPTablePage/index.vue'
import LPSearchBar from '@/components/LPSearchBar/index.vue'

export default {
  components: { LPSearchBar, LPTablePage },
  data() {
    return {
      searchModel: {},
      searchData: [
        {
          label: '',
          placeholder: '流水类型',
          prop: 'LType',
          type: 'select',
          option: [
            {
              label: '手动调整',
              value: '1'
            },
            {
              label: '销售出库',
              value: '2'
            },
            {
              label: '退货入库',
              value: '3'
            }
            // {
            //   label: '接口同步',
            //   value: '5'
            // },
          ]
        },
        {
          label: '',
          placeholder: '物料名称',
          prop: 'PTitle'
        },
        {
          label: '',
          placeholder: '单号',
          prop: 'OrdersNo',
          width: '80px'
        },
        {
          label: '',
          placeholder: '物料编码',
          prop: 'PNo'
        },
        {
          label: '',
          prop: 'StartOrderTime',
          type: 'datetimerange',
          placeholder: '开始时间',
          width: '100px'
        }
      ],
      // 列表数据
      tableData: [],
      // 表格字段
      tableColumn: [
        { label: '创建时间', prop: 'createTime' },
        { label: '物料编码', prop: 'pNo' },
        { label: '物料名称', prop: 'pTitle' },
        { label: '物料类型', prop: 'pClassName' },
        { label: '单位', prop: 'pUnit' },
        { label: '数量', prop: 'pCount' },
        { label: '流水类型', prop: 'lTypeName' },
        { label: '单号', prop: 'ordersNo', render: true },
        { label: '操作人', prop: 'createName' }
      ],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      bigType: [],
      centreType: [],
      smallType: [],
      // 详情是否弹窗
      dialogVisible: false
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 查询按钮
    handleSearchSubmit() {
      this.currentPage = 1
      this.getList()
    },
    // 重置按钮
    handleSearchReset() {
      this.handleSearchSubmit()
    },
    exportInfo() {
      const params = {
        ...this.searchModel
      }
      if (typeof this.searchModel.StartOrderTime !== 'undefined') {
        params.SDateTime = parseTime(this.searchModel.StartOrderTime[0])
        params.EDateTime = parseTime(this.searchModel.StartOrderTime[1])
        this.$delete(params, 'StartOrderTime')
      }
      GetProductInventoryLogExcel(params).then((res) => {
        download(res, '库存流水记录')
      })
    },
    clickSoNO(row) {
      console.log(row)
      if (row.lType == 3) {
        this.$router.push({
          path: '/laborProtection/order/returnOrderManage/detail',
          query: {
            data: JSON.stringify({
              id: row.ordersId,
              isAudit: false,
              lType: 2
            })
          }
        })
      } else if (row.lType == 2) {
        getByIdOrderData(row.ordersId).then((res) => {
          if (res.soType == 1) {
            this.$router.push({
              path: '/laborProtection/order/detail',
              query: {
                data: JSON.stringify({
                  id: row.ordersId,
                  isAudit: false,
                  lType: 2
                })
              }
            })
          } else {
            this.$router.push({
              path: '/remoteOrder/components/detail',
              query: {
                data: JSON.stringify({
                  id: row.ordersId,
                  isDetail: true,
                  lType: 2
                })
              }
            })
          }
        })
      }
    },
    // 获取列表
    getList() {
      // this.getCategoryInfo(1);
      // this.getCategoryInfo(2);
      // this.getCategoryInfo(3);
      const params = {
        SkipCount: this.currentPage,
        MaxResultCount: this.pageSize
      }
      if (typeof this.searchModel.StartOrderTime !== 'undefined') {
        params.SDateTime = parseTime(this.searchModel.StartOrderTime[0])
        params.EDateTime = parseTime(this.searchModel.StartOrderTime[1])
      }
      if (typeof this.searchModel.LType !== 'undefined') {
        params.LType = Number(this.searchModel.LType)
      }
      if (typeof this.searchModel.OrdersNo !== 'undefined') {
        params.OrdersNo = this.searchModel.OrdersNo
      }
      if (typeof this.searchModel.PNo !== 'undefined') {
        params.PNo = this.searchModel.PNo
      }
      if (typeof this.searchModel.PTitle !== 'undefined') {
        params.PTitle = this.searchModel.PTitle
      }
      getProductInventoryLogList(params).then((res) => {
        if (!res.error) {
          this.tableData = res.items
          this.total = res.totalCount
        }
      })
    },
    handleSizeChange(v) {
      this.pageSize = v
      this.currentPage = 1
      this.getList()
    },
    handleCurrentChange(v) {
      this.currentPage = v
      this.getList()
    },

    insertCommodity() {
      this.$router.push({
        path: './control'
      })
    }
  }
}
</script>

<style lang="scss">
.el-form-item__label {
  font-weight: normal;
}

.el-dialog__body {
  padding-top: 0px;
}

.el-divider {
  margin-top: 5px;
}
</style>

<style lang="scss" scoped>
.auditCmpt {
  display: flex;
  flex-direction: column;
  background-color: #f0f2f5;
  .form-baseinfo:first-child {
    margin: 20px, 0, 20px, -10px;
    border-radius: 3px;
  }
  .form-baseinfo:last-child {
    background-color: #fff;
    .btn_area {
      padding: 0.2rem;
      border-top-left-radius: 3px;
      border-top-right-radius: 3px;
    }
  }
}
.inventoryList {
  .form-baseinfo {
    ::v-deep .search-bar input.el-input__inner {
      width: 2rem;
    }
  }

  ::v-deep .table {
    .red {
      color: $high_risk_color;
    }
  }
}
</style>
